package com.md03.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.md03.entity.Teacher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TeacherDaoImpl implements TeacherDao {

    @Override
    public Teacher selectOneById(int id) {
        //这个采购，会去超市采购对应的食材
        //假设一个数组是一个超市
        Teacher[] teachers = {new Teacher(1, "小明"), new Teacher(2, "小米")};
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        DruidDataSource dataSource = context.getBean(DruidDataSource.class);
        try {
            DruidPooledConnection conn =  dataSource.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from teacher where t_id = " + id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return null;
    }

    //    public int addTeacher(Teacher teacher) {
//        return 0;
//    }
    @Override
    public int deleteTeacherById(int id) {
        //从数据库拿数据，通过druid连接池
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        DruidDataSource dataSource = context.getBean(DruidDataSource.class);

        try {
            DruidPooledConnection conn = dataSource.getConnection();
            Statement st = conn.createStatement();
            int i = st.executeUpdate("delete from teacher where t_id = " + id);
            return i;


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

//    public int editTeacher(Teacher teacher) {
//        return 0;
//    }

}
